unit uFormMain;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  uPluginIntf, StdCtrls, Dialogs,
  DrcpSvc;

type
  TForm_Main = class(TForm,IPluginable)
    ListBox_Users: TListBox;
    Label1: TLabel;
    Label2: TLabel;
    Edit_UserId: TEdit;
    Label3: TLabel;
    Edit_Password: TEdit;
    Label4: TLabel;
    ComboBox_AuthType: TComboBox;
    Label5: TLabel;
    Edit_UserName: TEdit;
    Label6: TLabel;
    Memo_Description: TMemo;
    Button_Update: TButton;
    Button_Delete: TButton;
    ListBox_JoinedRoles: TListBox;
    ListBox_UnjoinedRoles: TListBox;
    Button_Unjoin: TButton;
    Label7: TLabel;
    Label8: TLabel;
    Button_Join: TButton;
    Button_Refresh: TButton;
    procedure FormDestroy(Sender: TObject);
    procedure Button_RefreshClick(Sender: TObject);
    procedure ListBox_UsersClick(Sender: TObject);
    procedure Button_DeleteClick(Sender: TObject);
    procedure Button_UpdateClick(Sender: TObject);
    procedure Button_UnjoinClick(Sender: TObject);
    procedure Button_JoinClick(Sender: TObject);
  private
    FSoapAddr : string;
    FSoap : DrcpService;
    { Private declarations }

    function GetCurSelectedUser : User;
    function GetCurSelectedJoinedRole : Role;
    function GetCurSelectedUnjoinedRole : Role;

    procedure LoadUserList;

    procedure ClearListWithObject(AList : TStrings);
  protected
    FModeCreateNew : Boolean;
    FPlugin : IPlugin;
    procedure Init;

    procedure SetPlugin(APlugin : IPlugin);
    function GetPlugin:IPlugin;
  public
    { Public declarations }
    property Plugin : IPlugin read GetPlugin write SetPlugin;
  end;

var
  Form_Main: TForm_Main;
implementation

{$R *.dfm}

procedure TForm_Main.Init;
var
  lClientConfig : TStrings;
  lPrintPort : String;
begin
  { TODO -oyou -cplugin-template : fill initialization code here }

  //how to get client-config
  lClientConfig := TStringList.Create;
  lClientConfig.Text := Plugin.HostParams;

  lPrintPort := lClientConfig.Values['Port'];

  lClientConfig.Free;


  //how to get soap address by context variable, see more RS_xxx by CTL + LeftClick
  FSoapAddr := Plugin.Variable[RS_DEFAULT_CONTAINER_SOAP_ADDR];
  FSoap := GetDrcpService(false,FSoapAddr);

  //how to get initialzation info
  //Plugin.InitString can be any string, strore in db [module.initstring]

  LoadUserList;
end;

procedure TForm_Main.FormDestroy(Sender: TObject);
var
  lClientConfig : TStrings;
begin
  lClientConfig := TStringList.Create;
  lClientConfig.Values['Port'] := 'LPT1';
  lClientConfig.Values['X'] := '0';
  lClientConfig.Values['Y'] := '1';

  Plugin.HostParams := lClientConfig.Text;  //save to db

  lClientConfig.Free;

  { TODO -oyou -cplugin-template : fill finalization code here }
  
end;

procedure TForm_Main.Button_RefreshClick(Sender: TObject);
begin
  LoadUserList;
end;

procedure TForm_Main.ListBox_UsersClick(Sender: TObject);
var
  I : integer;
  lUser : User;
  lRole : Role;
  lJoinedRoleList,
  lAllRoleList : ArrayOf_tns2_Role;
begin
  lUser := User(ListBox_Users.Items.Objects[ListBox_Users.ItemIndex]);

  Edit_UserId.Text := lUser.id;
  Edit_UserName.Text := lUser.name;
  Memo_Description.Lines.Text := lUser.description;
  ComboBox_AuthType.ItemIndex := ComboBox_AuthType.Items.IndexOf(lUser.authType);
  Edit_Password.Text := lUser.password;

  ClearListWithObject(ListBox_JoinedRoles.Items);
  lJoinedRoleList := FSoap.UserRole_GetRoleList(lUser.id);
  for I := 0 to Length(lJoinedRoleList)-1 do
  begin
    lRole := Role(lJoinedRoleList[I]);
    ListBox_JoinedRoles.Items.AddObject(lRole.name,lRole);
  end;

  ClearListWithObject(ListBox_UnjoinedRoles.Items);
  lAllRoleList := FSoap.Role_GetAll;
  for I := 0 to Length(lAllRoleList)-1 do
  begin
    lRole := Role(lAllRoleList[I]);

    if ListBox_JoinedRoles.Items.IndexOf( lRole.id ) < 0 then //that not joined
      ListBox_UnjoinedRoles.Items.AddObject(lRole.name,lRole)
    else
      lRole.Free;
  end;

end;

procedure TForm_Main.Button_DeleteClick(Sender: TObject);
var
  lUser : User;
begin
  lUser := User(ListBox_Users.Items.Objects[ListBox_Users.ItemIndex]);
  FSoap.User_Delete(lUser.id);

  LoadUserList;
end;

procedure TForm_Main.LoadUserList;
var
  I : integer;
  lUser : User;
  lAllUser : ArrayOf_tns2_User;
begin

  ClearListWithObject(ListBox_Users.Items);

  lAllUser := FSoap.User_GetAll;
  for I := 0 to Length(lAllUser) -1 do
  begin
    lUser := lAllUser[I];
    ListBox_Users.Items.AddObject(lUser.id,lUser);
  end;
end;

procedure TForm_Main.Button_UpdateClick(Sender: TObject);
  function GetJoinedRoleIds: ArrayOf_xsd_string;
  var
    I : integer;
  begin
    SetLength(Result,ListBox_JoinedRoles.Count);
    for I:= 0 to ListBox_JoinedRoles.Count-1 do
      Result[I] := Role(ListBox_JoinedRoles.Items.Objects[I]).id;

  end;
var
  lUser : User;
begin
  lUser := User.Create;

  lUser.id := Edit_UserId.Text;
  lUser.name := Edit_UserName.Text;
  lUser.description := Memo_Description.Lines.Text;
  lUser.authType := ComboBox_AuthType.Text;
  lUser.password := Edit_Password.Text;

  if lUser.id = GetCurSelectedUser.id then
    FSoap.User_Update(lUser)
  else
    FSoap.User_Save(lUser);

  //update user-role set
  FSoap.UserRole_SetRoleList(lUser.id,GetJoinedRoleIds());

  LoadUserList;
end;

function TForm_Main.GetCurSelectedUser: User;
begin
  Result := User(ListBox_Users.Items.Objects[ListBox_Users.ItemIndex]);
end;

procedure TForm_Main.ClearListWithObject(AList: TStrings);
var
  I : integer;
begin
  for I := 0 to AList.Count -1 do
    (AList.Objects[I]).Free;

  AList.Clear;
end;

function TForm_Main.GetCurSelectedJoinedRole: Role;
begin
  Result := Role(ListBox_JoinedRoles.Items.Objects[ListBox_JoinedRoles.ItemIndex]);
end;

function TForm_Main.GetCurSelectedUnjoinedRole: Role;
begin
  Result := Role(ListBox_UnjoinedRoles.Items.Objects[ListBox_UnjoinedRoles.ItemIndex]);
end;

procedure TForm_Main.Button_UnjoinClick(Sender: TObject);
var
  lRole : Role;
begin
  lRole := nil;
  lRole := GetCurSelectedJoinedRole;
  if Role = nil then
  begin
    ShowMessage('Select Role first!');
    Exit;
  end;

  //remove from left
  ListBox_JoinedRoles.Items.Delete(ListBox_JoinedRoles.ItemIndex);
  //add to right
  ListBox_UnjoinedRoles.Items.AddObject(lRole.name,lRole);

end;

procedure TForm_Main.Button_JoinClick(Sender: TObject);
var
  lRole : Role;
begin
  lRole := nil;
  lRole := GetCurSelectedUnjoinedRole;
  if Role = nil then
  begin
    ShowMessage('Select Role first!');
    Exit;
  end;

  //remove from left
  ListBox_unJoinedRoles.Items.Delete(ListBox_unJoinedRoles.ItemIndex);
  //add to right
  ListBox_joinedRoles.Items.AddObject(lRole.name,lRole);


end;

function TForm_Main.GetPlugin: IPlugin;
begin
  Result := FPlugin;
end;

procedure TForm_Main.SetPlugin(APlugin: IPlugin);
begin
  FPlugin := APlugin;

  init;
end;

end.

